package sort;

/**
 * 二分查找法
 */
public class TwoDivideSerach {

    public static void main(String args[]){

        //默认输入的是从小到大已排序好的数据
        int[] nums=new int[]{1,2,3,4,5,6,7,8,9,10,90,100,200};
        int index= serach(nums,90,0,nums.length-1);
        System.out.println("搜索到数组下标："+index);

    }

    public static int serach(int nums[],int key,int begin,int end) {

        if (begin == end) {
            if (nums[begin] == key) {
                return begin;
            } else {
                return -1;
            }
        }

        int mid = (begin + end) / 2;
        if (nums[mid] > key) {
          return   serach(nums, key, begin, mid);
        } else if (nums[mid] < key) {
          return  serach(nums, key, mid, end);
        } else {
            //相等
          return mid;
        }
    }
}
